import Vue from 'vue';
import VueRouter from 'vue-router';
/* import { getToken } from '@/tokentest.js'; */

Vue.use(VueRouter);
// meta: {
// 			title: 侧边栏名称
// 			icon: 图标
// 			show: 是否显示在侧边栏
// 			click: 是否可以点击后跳转路由
// 		},

const routes = [
	{
		path: '/login',
		name: 'login',
		meta: {
			title: '登录',
			icon: 'el-icon-menu',
			show: false,
			click: false,
		},
		component: () => import('../views/login/index.vue'),
	},
	{
		path: '/',
		redirect: '/home',
		name: 'layout',
		component: () => import('../layout/index.vue'),
		children: [
			{
				path: 'home',
				name: 'home',
				meta: {
					title: '首页',
					icon: 'el-icon-menu',
					show: true,
					click: true,
				},
				component: () => import('@/views/home/index.vue'),
			},
			{
				path: 'new',
				name: 'new',
				meta: {
					title: '校园新闻',
					icon: 'el-icon-setting',
					show: true,
					click: true,
				},
				component: () => import('@/views/new/index.vue'),
			},
			{
				path: 'student',
				name: 'student',
				meta: {
					title: '学生管理',
					icon: 'el-icon-document',
					show: true,
					click: false,
				},
				component: () =>
					import('@/views/student/index.vue'),
				children: [
					{
						path: 'list',
						name: 'StudentList',
						meta: {
							title: '学生列表',
							show: true,
							click: true,
						},
						component: () =>
							import('@/views/student/list/index.vue'),
					},
					{
						path: 'form',
						name: 'StudentAdd',
						meta: {
							title: '学生添加',
							show: true,
							click: true,
						},
						component: () =>
							import('@/views/student/form/index.vue'),
					},
				],
			},
		],
	},
	{
		path: '/register',
		name: 'register',
		component: () => import('@/views/register/index.vue'),
	},
];

const router = new VueRouter({
	routes,
});

// 添加路由守卫
/* router.beforeEach((to, from, next) => {
	// 白名单路径（允许未登录访问）
	const whiteList = ['/login', '/'];

	// 检查登录状态（使用封装的token方法）
	const hasToken = getToken(); // 假设getToken是封装的token方法

	if (hasToken) {
		// 已登录状态
		if (to.path === '/login') {
			next({ path: '/' }); // 已登录跳转到首页
		} else {
			next(); // 正常访问
		}
	} else {
		// 未登录状态
		if (whiteList.includes(to.path)) {
			next(); // 允许访问白名单页面
		} else {
			next(`/login`); // 重定向到登录页
		}
	}
}); */

export default router;
